library(tidyverse)
library(tidymodels)
library(readstata13)
library(marginaleffects)
library(modelsummary)
library(margins)
library(dplyr)
library(stargazer)
library(ggpubr)

rm(list = ls())


#Data Read in#### 

data1<-read.dta13("Code/Study3/Vignette/merged_study3.dta", nonint.factors=T)
summary(data1)

data1 <- data1 %>% select(pid3_lean_w2, 
                          pollwatch_1_w2_rev,
                          pollwatch_2_w2_rev,
                          pollwatch_3_w2_rev,
                          pollwatch_4_w2_rev,
                          pollwatch_5_w2_rev, 
                          abilities_1_w2,
                          abilities_2_w2,
                          abilities_3_w2,
                          abilities_4_w2,
                          treat_cond, q49_1, q49_2, q49_3, q49_4, q49_5,q35, familiar) %>% 
  filter(treat_cond!="NA") %>% 
  mutate(treat_cond=recode_factor(treat_cond, 
                                  "Poll Watcher Normal" = "Good Faith",
                                  "Poll Watcher Aggressive" = "Bad Faith"))

summary(data1)

#Outcome 1 - Ensure Election Works Do a Good Job#### 
mod_out1<-lm(pollwatch_1_w2_rev~1+treat_cond*pid3_lean_w2+q49_1, data=data1)

#Outcome 2 - Ensure Voters Follow Rules#### 
mod_out2<-lm(pollwatch_2_w2_rev~1+treat_cond*pid3_lean_w2+q49_2, data=data1)

#Outcome 3 - Ensure Party Officials Do Not Manipulate Results#### 
mod_out3<-lm(pollwatch_3_w2_rev~1+treat_cond*pid3_lean_w2+q49_3, data=data1)

#Outcome 4 - Ensure Election Officials Follow Rules#### 
mod_out4<-lm(pollwatch_4_w2_rev~1+treat_cond*pid3_lean_w2+q49_4, data=data1)

#Outcome 5 - Intimidate Voters####
mod_out5<-lm(pollwatch_5_w2_rev~1+treat_cond*pid3_lean_w2+q49_5, data=data1)

##Calculating ATE Differences####

diff_out1a<-comparisons(mod_out1, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q1: Do Job")

diff_out1b<-comparisons(mod_out1, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q1: Do Job")

diff_out2a<-comparisons(mod_out2, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q2: Rules (Voters)")
diff_out2b<-comparisons(mod_out2, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q2: Rules (Voters)")

diff_out3a<-comparisons(mod_out3, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q3: No Manipulation")
diff_out3b<-comparisons(mod_out3, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q3: No Manipulation")
diff_out4a<-comparisons(mod_out4, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q4: Rules (Officials)")
diff_out4b<-comparisons(mod_out4, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q4: Rules (Officials)")

diff_out5a<-comparisons(mod_out5, 
                       variables=list(treat_cond=c("Control","Good Faith")),
                       newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q5: Intimidate")
diff_out5b<-comparisons(mod_out5, 
                       variables=list(treat_cond=c("Control","Bad Faith")),
                       newdata=datagrid(pid3_lean_w2=c("Repub","Dem"))) %>% add_column(question="Q5: Intimidate")

#Hypothesis Tests (diff in diff) 
comparisons(mod_out1, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")

comparisons(mod_out1, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")

comparisons(mod_out2, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")
comparisons(mod_out2, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")

comparisons(mod_out3, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")
comparisons(mod_out3, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")

comparisons(mod_out4, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")
comparisons(mod_out4, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")

comparisons(mod_out5, 
                        variables=list(treat_cond=c("Control","Good Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")
comparisons(mod_out5, 
                        variables=list(treat_cond=c("Control","Bad Faith")),
                        newdata=datagrid(pid3_lean_w2=c("Repub","Dem")),
                        hypothesis="pairwise")


diff_combined<-bind_rows(diff_out1a, diff_out1b,diff_out2a, diff_out2b,diff_out3a, diff_out3b,diff_out4a, diff_out4b,diff_out5a, diff_out5b)

diff_combined<-diff_combined %>% mutate(contrast=case_when( 
  contrast=="Good Faith - Control" ~ "Good Faith-\nControl",
  contrast=="Bad Faith - Control" ~ "Bad Faith-\nControl"))

dat_text <- data.frame(
  label = c("*", "*", "*"),
  question   = c("Q5: Intimidate", "Q3: No Manipulation", "Q1: Do Job"),
  pid3_lean_w2=c("Repub", "Dem","Dem"),
  x=c(1,2,2),
  y=c(1.2,.39,.3))

pd <- position_dodge(.30)
x<-ggplot(diff_combined, aes(x=contrast, y=estimate, group=pid3_lean_w2, shape=pid3_lean_w2))
plot5<-x+geom_errorbar(aes(ymin=conf.low, ymax=conf.high), position=pd, width=0)+
  geom_point(aes(fill=pid3_lean_w2), stat="identity", position=pd, size=4)+
  scale_shape_manual(values=c(22,21))+
  #scale_fill_brewer(palette="Greys")+
  geom_hline(yintercept=0)+
  facet_grid(~question)+
  scale_fill_manual(values=c("grey","black"))+
  #scale_y_continuous(limits=c(2.0,4.5))+
  theme(axis.title=element_text(size=14,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme_bw()+
  #ggtitle("Q5 - Intimidate Voters")+
  xlab("Contrast")+
  ylab("Difference")+
  theme(legend.title=element_blank(), legend.position="top")+
  theme(legend.text=element_text(size=14))+
  theme(axis.title=element_text(size=16,face="bold"))+
  theme(axis.text=element_text(size=14))+
  theme(strip.text.x = element_text(size = 14))+ 
  geom_text(
    data    = dat_text,
    mapping = aes(x = x, y = y, label = label),
    size=12
  )
plot5

ggsave(filename="Figures/Main Paper/study3_ATE.tiff",
       device="tiff",
       width=12, 
       height=7, 
       dpi=135)  

##Appendix####
#Outputting tables#### 
#Table F1 
stargazer(mod_out1, mod_out2, mod_out3, mod_out4, mod_out5,
          dep.var.labels=c("1","2","3","4","5"),
          covariate.labels=c("Bad Faith Treat","Control Condition", 
                             "PID - Ind.", "PID - Repub.", 
                             "Wave 1 Outcome (Honest)",
                             "Wave 1 Outcome (Rules)",
                             "Wave 1 Outcome (Partisan)",
                             "Wave 1 Outcome (Officals Rules)",
                             "Wave 1 Outcome (Intimidate)",
                             "Bad Faith X Ind.","Control X Ind.", 
                             "Bad Faith X Repub","Control X Repub"),
          column.labels = c("Honesty","Follow Rules", 
                            "Manipulation","Enforce Rules",
                            "Intimidate"),
          model.numbers = F,
          dep.var.labels.include=F,
          dep.var.caption="",
          keep.stat = c("adj.rsq", "n"),
          star.cutoffs = c(0.05, 0.01, 0.001),
          type="html", out="Tables/TableF1.html")

#Table F2 
mod_out1<-lm(pollwatch_1_w2_rev~1+treat_cond*pid3_lean_w2, data=data1)
mod_out2<-lm(pollwatch_2_w2_rev~1+treat_cond*pid3_lean_w2, data=data1)
mod_out3<-lm(pollwatch_3_w2_rev~1+treat_cond*pid3_lean_w2, data=data1)
mod_out4<-lm(pollwatch_4_w2_rev~1+treat_cond*pid3_lean_w2, data=data1)
mod_out5<-lm(pollwatch_5_w2_rev~1+treat_cond*pid3_lean_w2, data=data1)

stargazer(mod_out1, mod_out2, mod_out3, mod_out4, mod_out5,
          dep.var.labels=c("1","2","3","4","5"),
          covariate.labels=c("Bad Faith Treat","Control Condition", 
                             "PID - Ind.", "PID - Repub.", 
                             "Bad Faith X Ind.","Control X Ind.", 
                             "Bad Faith X Repub","Control X Repub"),
          column.labels = c("Honesty","Follow Rules", 
                            "Manipulation","Enforce Rules",
                            "Intimidate"),
          model.numbers = F,
          dep.var.labels.include=F,
          dep.var.caption="",
          keep.stat = c("adj.rsq", "n"),
          star.cutoffs = c(0.05, 0.01, 0.001),
          type="html", out="Tables/TableF2.html")


#Table F3 
mod_out1<-lm(pollwatch_1_w2_rev~1+treat_cond*pid3_lean_w2, data=subset(data1, familiar=="2"))
mod_out2<-lm(pollwatch_2_w2_rev~1+treat_cond*pid3_lean_w2, data=subset(data1, familiar=="2"))
mod_out3<-lm(pollwatch_3_w2_rev~1+treat_cond*pid3_lean_w2, data=subset(data1, familiar=="2"))
mod_out4<-lm(pollwatch_4_w2_rev~1+treat_cond*pid3_lean_w2, data=subset(data1, familiar=="2"))
mod_out5<-lm(pollwatch_5_w2_rev~1+treat_cond*pid3_lean_w2, data=subset(data1, familiar=="2"))

stargazer(mod_out1, mod_out2, mod_out3, mod_out4, mod_out5,
          dep.var.labels=c("1","2","3","4","5"),
          covariate.labels=c("Bad Faith Treat", 
                             "PID - Ind.", "PID - Repub.", 
                             "Bad Faith X Ind.",
                             "Bad Faith X Repub",
                             "Constant"),
          column.labels = c("Honesty","Follow Rules", 
                            "Manipulation","Enforce Rules",
                            "Intimidate"),
          model.numbers = F,
          dep.var.labels.include=F,
          dep.var.caption="",
          keep.stat = c("adj.rsq", "n"),
          star.cutoffs = c(0.05, 0.01, 0.001),
          type="html", out="Tables/TableF3.html")